package expressToAltaRica;

import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import org.w3c.dom.*;

import javax.xml.parsers.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.SAXException;

public class Test1 {

	public static  void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException{

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc1 = builder.parse("mytest.xml");
				
		 NodeList activatorList = doc1.getElementsByTagName("Mfha_activator");
		 NodeList ioList = doc1.getElementsByTagName("Mfha_out");
		 for(int i = 0;i<activatorList.getLength();i++){
			 Element activator = (Element) activatorList.item(i);
			 Element act = (Element) activator.getFirstChild();
			 String act_id = act.getAttributeNode("ref").getValue();
			 
			 for(int j=0;j<ioList.getLength();j++){
				 Element io = (Element) ioList.item(j);
				 String io_id = io.getAttributeNode("id").getValue();
				 if(act_id.equalsIgnoreCase(io_id)){
					 act.getParentNode().replaceChild(io,act);
				 } 
			 }
			 
		 }
			
		DocumentBuilderFactory factory1 = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory1.newDocumentBuilder();
		Document doc = parser.newDocument();
		Element export = doc.createElement("export");
		doc.appendChild(export);
				 
		Element activator = (Element) activatorList.item(0);		
		Element component_family = doc.createElement("COMPONENT_FAMILY");
	    Element id_component_family = doc.createElement("IDENTIFICATION");
		Element component_model = doc.createElement("COMPONENT_MODEL");
	    Element id_component_model = doc.createElement("IDENTIFICATION");
	    export.appendChild(component_family);
	    component_family.appendChild(id_component_family);
	    component_family.appendChild(component_model);
	    component_model.appendChild(id_component_model);
	    
	    //traitement des attributs du component_model
	    String[] name_c_m_list = {"BORDER","HEIGHT","WIDTH","AUTOMOVEPORT","ICON","LAW"};
	    //tous les attributs de lidentification
	    String[] name_id_list = {"NAME","VERSION","RELEASE","CREATION_DATE","UPDATE_DATE","GROUP_ACCESS","OTHER_ACCESS","GROUP","OWNER","COMMENT","FREEZE"};
	    //tous les attributs des component
	    String[] name_component_list = {"NAME","FAMILY","MODEL","VERSION","X","Y","TEXT_LOC","SHEET","LOCKED"};
	  //tous les attributs des sheet
	    String[] name_sheet_list = {"NAME","NUMBER","SELECTED","VISIBLE","ORDER"};
	    //traitement des valeurs des component_family
	    String[] value_id_c_f_list = {"root","","","","","1","0","admins","admin","","false"};
	    //traitement des valeurs des component_model
	    String[] value_id_c_m_list = {"Activator","1","1","","","1","0","admins","admin","To start all chain.","false"};
	    //traitement des valeurs du project
	    String[] value_id_project_list = {"Test_transfo","","","","","1","0","admins","admin","","false"};
	    //traitement des valeurs du system
	    String[] value_id_system_list = {"Example_1","1","10","","","1","0","admins","admin","","false"};
	    //traitement des valeurs du component_model
	    String[] value_c_m_list = {"false","40","64","true","Root current/icons/act_source_GIF.png","assert&#10;&#9;    out_act = true;&#10;"};
	  //traitement des valeurs de la sheet1
	    String[] value_sheet1_list = {"Layer 1","1","true","true","1"};
	  //traitement des valeurs de la sheet2
	    String[] value_sheet2_list = {"Layer 2","2","false","true","2"};
	  //traitement des valeurs de la sheet3
	    String[] value_sheet3_list = {"Layer 3","3","false","false","3"};
	  //traitement des valeurs de la sheet4
	    String[] value_sheet4_list = {"Layer 4","4","false","false","4"};
	  //traitement des valeurs de la sheet5
	    String[] value_sheet5_list = {"Layer 5","5","false","false","5"};
	    
	    for(int k = 0; k<name_id_list.length ; k++){
    		id_component_family.setAttribute(name_id_list[k],value_id_c_f_list[k]);
    	}
	    for(int k = 0; k<name_c_m_list.length ; k++){
    		component_model.setAttribute(name_c_m_list[k],value_c_m_list[k]);
    	}
	
	    Element io_interface = (Element) activator.getFirstChild();
	    Element io  = doc.createElement("IO");
	    component_model.appendChild(io);
	    io.setAttribute("NAME",io_interface.getFirstChild().getTextContent());
	    io.setAttribute("TYPE", "bool");
		if(io_interface.getNodeName().endsWith("out")){
		   	io.setAttribute("DIRECTION", "out");
		}else{
			io.setAttribute("DIRECTION", "in");
		}
	    io.setAttribute("X", "58");
	    io.setAttribute("Y", "16");
	    
	   
	    for(int k = 0; k<name_id_list.length ; k++){
    		id_component_model.setAttribute(name_id_list[k],value_id_c_m_list[k]);
    	}	    	    
		
	    Element project = doc.createElement("PROJECT");
	    Element id_project = doc.createElement("IDENTIFICATION");
	    Element system = doc.createElement("SYSTEM");
	    Element id_system = doc.createElement("IDENTIFICATION");
	    Element architecture = doc.createElement("ARCHITECTURE");
	    Element color_bool = doc.createElement("COLOR");
	    Element color_effect = doc.createElement("COLOR");
	    Element init_file = doc.createElement("INIT_FILE");
	    export.appendChild(project);
	    project.appendChild(id_project);
	    project.appendChild(system);
	    system.appendChild(architecture);
	    system.appendChild(id_system);
	    system.appendChild(color_bool);
	    color_bool.setAttribute("NAME","bool");
	    system.appendChild(color_effect);
	    color_effect.setAttribute("NAME","Functional_effectiveness/effectiveness_5levels");
	    system.appendChild(init_file);

	    //creer les 5 sheets
		Element sheet1 = doc.createElement("SHEET");
		system.appendChild(sheet1);
   		for(int k=0; k<name_sheet_list.length ; k++){
   			sheet1.setAttribute(name_sheet_list[k],value_sheet1_list[k]);
	    }
		Element sheet2 = doc.createElement("SHEET");
		system.appendChild(sheet2);
   		for(int k=0; k<name_sheet_list.length ; k++){
   			sheet2.setAttribute(name_sheet_list[k],value_sheet2_list[k]);
	    }
   		Element sheet3 = doc.createElement("SHEET");
		system.appendChild(sheet3);
   		for(int k=0; k<name_sheet_list.length ; k++){	
   			sheet3.setAttribute(name_sheet_list[k],value_sheet3_list[k]);
	    }
		Element sheet4 = doc.createElement("SHEET");
		system.appendChild(sheet4);
   		for(int k=0; k<name_sheet_list.length ; k++){
   			sheet4.setAttribute(name_sheet_list[k],value_sheet4_list[k]);
	    }
		Element sheet5 = doc.createElement("SHEET");
		system.appendChild(sheet5);
   		for(int k=0; k<name_sheet_list.length ; k++){
   			sheet5.setAttribute(name_sheet_list[k],value_sheet5_list[k]);
	    }
	    	    
   		//creer les attributs de lidentification du projet
	    for(int k = 0; k<name_id_list.length ; k++){
    		id_project.setAttribute(name_id_list[k],value_id_project_list[k]);
    	}
	    
	    architecture.setAttribute("ASSERTION","");

	    for(int j=0 ; j<activatorList.getLength() ; j++){
	    	Element component = doc.createElement("COMPONENT");
	    	architecture.appendChild(component);
	    	for(int l = 0; l<name_component_list.length ; l++){
	    		String[] value_list = {activatorList.item(j).getLastChild().getTextContent(),"root","Activator","1","26","109","0","1","false"};
	    		component.setAttribute(name_component_list[l],value_list[l]);
	    	}
	    }
	    
	    for(int k = 0; k<name_id_list.length ; k++){
    		id_system.setAttribute(name_id_list[k],value_id_system_list[k]);
    	}
	    
	    //Transformation et creation du document final		  
		TransformerFactory tfact = TransformerFactory.newInstance();
		Transformer transformer = tfact.newTransformer();
		transformer.setOutputProperty("encoding", "ISO-8859-1");
	    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource source = new DOMSource(doc);
		FileWriter fw = new FileWriter("nouveaufichier.xml");
		StreamResult result = new StreamResult(fw);
		transformer.transform(source, result);
		
	       }
		 
	}
	


